package com.sun.commons.base;

import java.io.Serializable;
import java.util.LinkedHashMap;

/**
 * MP自动填充Handler
 *
 * @Author sun
 * @CreateTime 2025/9/6 22:22
 */
public class R extends LinkedHashMap<String, Object> implements Serializable {
    private static final long serialVersionUID = 1L;
    public static final String SUCCESS = "200";
    public static final String ERROR = "500";
    public static final String UNAUTHORIZED = "401";
    public static final String FORBIDDEN = "403";
    public static final String NOT_FOUND = "404";
    public static final String BAD_REQUEST = "400";
    public static final String INTERNAL_SERVER_ERROR = "500";

    public R() {
    }

    public R(String code, String msg) {
        this.setCode(code);
        this.setMsg(msg);
    }



    public R set(String key, Object value) {
        this.put(key, value);
        return this;
    }

    public R setData(Object data) {
        this.put("data", data);
        return this;
    }

    public R setCode(String code) {
        this.put("code", code);
        return this;
    }

    public R setMsg(String msg) {
        this.put("msg", msg);
        return this;
    }

    public static R ok() {
        return new R(SUCCESS, "操作成功");
    }

    public static R ok(String msg) {
        return new R(SUCCESS, msg);
    }





    public static R fail() {
        return new R(ERROR, "操作失败");
    }

    public static R fail(String msg) {
        return new R(ERROR, msg);
    }

    public static R fail(String code, String msg) {
        return new R(code, msg);
    }

    public static R noLogin() {
        return new R(UNAUTHORIZED, "未登录");
    }

    public static R noPermission() {
        return new R(FORBIDDEN, "没有权限");
    }

    public static R notFound() {
        return new R(NOT_FOUND, "没有找到相关资源");
    }

    public static R badRequest() {
        return new R(BAD_REQUEST, "请求参数错误");
    }

    public static R internalServerError() {
        return new R(INTERNAL_SERVER_ERROR, "服务器内部错误");
    }

    public static R tokenNotValid() {
        return new R(UNAUTHORIZED, "token失效");
    }

    public static R tokenExpired() {
        return new R(UNAUTHORIZED, "token过期");
    }

    public static R tokenError() {
        return new R(UNAUTHORIZED, "token错误");
    }

    public static R tokenEmpty() {
        return new R(UNAUTHORIZED, "token为空");
    }

    public static R tokenInvalid() {
        return new R(UNAUTHORIZED, "token无效");
    }
}
